import * as config from '../../config'

const jweixin = require('./jssdk-1.4.1')

const configWeiXin = async function(callback) {
	//console.log(window.location.href);
	uni.request({
		url: config.def().baseUrl + '/api/member/getH5Sign',
		data: {
			url: window.location.href
		},
		method: 'POST',
		// header: {
		// 	'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		// },
		dataType: 'json',
		success: (res) => {
			console.log(res);
			if (res.data.code === 200) {
				let resConfig = res.data.data;
				if (resConfig) {
					let apiList = [ // 可能需要用到的能力
						'checkJsApi',
						'chooseWXPay',
						'onMenuShareAppMessage',
						'onMenuShareTimeline',
						'updateAppMessageShareData'
					];
					let info = {
						debug: config.def().debug, // 调试，发布的时候改为false
						appId: config.def().h5Appid,
						nonceStr: resConfig.noncestr,
						timestamp: resConfig.timestamp,
						signature: resConfig.sign,
						jsApiList: apiList
					};
					jweixin.config(info);
					jweixin.error(err => {
						//console.log('config fail:', err);
					});

					jweixin.ready(res => {
						if (callback) callback(jweixin); // 配置成功
					});
				}
			}
		}
	})

}

export {
	configWeiXin
}
